package com.server.llh.llhserverwork.view.widget;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class MyTimePickerView {
	/* 小时 */private static int mHour;
	/* 分钟 */private static int mMinute;
	/* 标识 dialog的id */static final int TIME_DIALOG_ID = 0;
	static TextView tvTimePickerDisplay;
	static Activity context;

	public static void setTime(Activity mContext, TextView myTextView) {
		context = mContext;
		tvTimePickerDisplay = myTextView;
		final Calendar c = Calendar.getInstance();
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);
		updateDisplay(tvTimePickerDisplay);
	}

	static void updateDisplay(TextView tvTimePickerDisplay) {
		tvTimePickerDisplay.setText(new StringBuilder().append(pad(mHour))
				.append(":").append(pad(mMinute)));
	}

	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}

	// the callback received when the user "sets" the time in the dialog
	private static TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour = hourOfDay;
			mMinute = minute;
			updateDisplay(tvTimePickerDisplay);
		}
	};

	public static Dialog onCreateDialog(int id) {

		if (id == TIME_DIALOG_ID) {
			return new TimePickerDialog(context, mTimeSetListener, mHour,
					mMinute, true);
		}

		return null;
	}

}
